/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import Connection.ConnectionDB;
import Connection.UploadImage;
import enteties.Image;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javassist.bytecode.stackmap.BasicBlock;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author Trung Kien
 */
public class UploadImageServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletContext servletContext = this.getServletConfig().getServletContext();
            File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");

            factory.setRepository(repository);
            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            
            List<FileItem> items = fileUpload.parseRequest(request);
            String image = "",
                   dayshooting = "",
                   userid = "",
                   albumid = "",
                   introduction = "";
            for (FileItem fi : items) {
                if (fi.isFormField()) {
                    String key = fi.getFieldName();
                    String value = fi.getString("UTF-8");

                    if (key.equals("imgName")) {
                        image = value;
                    }
                    if(key.equals("datshooting"))
                    {
                        dayshooting = value;
                    }
                    if(key.equals("userid"))
                    {
                        userid = value;
                    }
                    if(key.equals("albumid"))
                    {
                        albumid = value;
                    }
                    if(key.equals("introduction"))
                    {
                        introduction = value;
                    }
                    
                }
                else{
                try {
                        String key = fi.getFieldName();
                        image = fi.getName();
                        String path = request.getSession().getServletContext().getRealPath("uploads");
                        File uploadLocation = new File(path + "/" + image);
                        fi.write(uploadLocation);
                    } catch (Exception ex) {
                        Logger.getLogger(UploadImageServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            Image i = new Image(image, dayshooting,Integer.parseInt(userid) ,Integer.parseInt(albumid), introduction);
            String url = getServletContext().getInitParameter("DB_URL");
            String user = getServletContext().getInitParameter("DB_USER");
            String dbpassword = getServletContext().getInitParameter("DB_PASSWORD");
            
            UploadImage u = new UploadImage(url, user, dbpassword);
            u.CreatImage(i);
            response.sendRedirect("index.jsp");
            
        } catch (FileUploadException ex) {
            Logger.getLogger(UploadImageServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
